#include <QtGui>

#include "src/color.h"
#include "src/spring.h"
#include "springDialog.h"
#include "springTreeItem.h"

SpringDialog::SpringDialog(SpringTreeItem *_item, QWidget *_parent)
    : QDialog(_parent), item(_item){

    spring = item->getSpring();
    unsigned int id = spring->getID();
    setWindowTitle(QString::fromUtf8("Modificando a mola ").append(QString::number(id)));

    Color color = spring->getActualColor();
    colorR = color.r * 255;
    colorG = color.g * 255;
    colorB = color.b * 255;
    colorAlpha = color.alpha * 255;

    ks = spring->getKs();
    kd = spring->getKd();

    createLayout();
}

SpringDialog::~SpringDialog(){
}

void SpringDialog::createLayout(){
    //--------------------------------------Constantes
    QDoubleSpinBox *ksDoubleSpinBox = new QDoubleSpinBox();
    ksDoubleSpinBox->setRange(0.000001, 50000.0);
    ksDoubleSpinBox->setSingleStep(1.0);
    ksDoubleSpinBox->setDecimals(6);
    ksDoubleSpinBox->setValue(ks);
    connect(ksDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeKs(double)));

    QDoubleSpinBox *kdDoubleSpinBox = new QDoubleSpinBox();
    kdDoubleSpinBox->setRange(0.0, 50000.0);
    kdDoubleSpinBox->setSingleStep(1.0);
    kdDoubleSpinBox->setDecimals(6);
    kdDoubleSpinBox->setValue(kd);
    connect(kdDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeKd(double)));

    QHBoxLayout *ksLayout = new QHBoxLayout;
    ksLayout->addWidget(new QLabel ( QString::fromUtf8("Ks: ") ));
    ksLayout->addWidget(ksDoubleSpinBox);

    QHBoxLayout *kdLayout = new QHBoxLayout;
    kdLayout->addWidget(new QLabel ( QString::fromUtf8("Kd: ") ));
    kdLayout->addWidget(kdDoubleSpinBox);

    QVBoxLayout *kLayout = new QVBoxLayout;
    kLayout->addLayout(ksLayout);
    kLayout->addLayout(kdLayout);
    kLayout->addStretch(1);

    QGroupBox *kGroupBox = new QGroupBox(QString::fromUtf8("Constantes"));
    kGroupBox->setLayout(kLayout);


    //--------------------------------------Cor
    colorPushButton = new QPushButton(tr("Cor"));
    connect(colorPushButton, SIGNAL(clicked()), this, SLOT(changeColor()));
    setButtonColor();






    //--------------------------------------Botões
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptChange()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    buttonBox->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil));

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(kGroupBox);
    mainLayout->addWidget(colorPushButton);
    mainLayout->addWidget(buttonBox);
    mainLayout->addStretch(1);

    setLayout(mainLayout);
}

void SpringDialog::setButtonColor(){
    //    QString colorStr = QString::fromUtf8("color: rgb(255, 255, 255);\n" "background-color: rgb(85, 0, 0, 255);");
    QString colorStr;
    if(colorR + colorB + colorG + colorAlpha < 600){
        colorStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
    }else{
        colorStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
    }
    colorStr.append(QString::fromUtf8("background-color: rgb("));
    colorStr.append(QString::number(colorR));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorG));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorB));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(colorAlpha));
    colorStr.append(QString::fromUtf8(");"));

    colorPushButton->setStyleSheet(colorStr);
}

void SpringDialog::acceptChange(){
    spring->setColor(Color(colorR / 255.0, colorG / 255.0, colorB / 255.0, colorAlpha / 255.0));
    spring->setKsAndKd(ks, kd);

    emit done();

    accept();
}

void SpringDialog::changeColor(){
    QColor color = QColorDialog::getColor(QColor(colorR, colorG, colorB, colorAlpha), this, "Escolha a cor", QColorDialog::ShowAlphaChannel);
    if (color.isValid()){
        colorR = color.red();
        colorG = color.green();
        colorB = color.blue();
        colorAlpha = color.alpha();
    }
    setButtonColor();
}

void SpringDialog::changeKs(double _ks){
    ks = _ks;
}

void SpringDialog::changeKd(double _kd){
    kd = _kd;
}

